// pages/editMemorandum/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    title: '',
    details: '',
    checked: false,
    isUpload: false,
    fileLength: 0,
    fileList: [],
    userId: '',
    detailUserId: '',
    disabled: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id) {
      this.setData({ id: options.id, userId: wx.getStorageSync('userId') })
      this.getDetail(options.id);
    }else {
      if(wx.getStorageSync('editMemorandum')) {
        this.setData({ details: wx.getStorageSync('editMemorandum') });
      }
      if(wx.getStorageSync('editMemorandumTitle')) {
        this.setData({ title: wx.getStorageSync('editMemorandumTitle') });
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  getDetail(id) {
    wx.request({
      url: `${app.globalData.baseUrl}/api/memo/selMemoById?id=${id}`,
      method: "GET",
      header: {
        'token': wx.getStorageSync('token'),
        "Content-Type": "application/json"
      },
      success: (res) => {
        if (res.data.code == 0) {
          const { title, details, opens, picture, userId } = res.data.data;
          this.setData({ 
            title,
            details,
            detailUserId: userId,
            checked: opens === '1' ? true : false,
            fileList: picture ? picture.split(',') : []
          });
        }
      }
    })
  },

  onChange(e) {
    this.setData({ checked: e.detail })
  },

  titleInput(e) {
    this.setData({ title: e.detail });
    wx.setStorageSync('editMemorandum', e.detail);
  },

  detailsInput(e) {
    this.setData({ details: e.detail.value });
    wx.setStorageSync('editMemorandumTitle', e.detail.value);
  },

  uploadShow: function (e) {
    this.setData({
      isUpload: true,
      fileLength: this.data.fileList.length
    });
  },

  preview(e) {
    const currentUrls = [...this.data.fileList];
    const index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: currentUrls[index], // 当前显示图片的http链接
      urls: [...currentUrls] // 需要预览的图片http链接列表
    })
  },

  deleteList(e) {
    const list = [...this.data.fileList];
    this.setData({ fileList: list.filter(item => item !=  e.currentTarget.dataset.url)});
  },

  receivingData: async function (val) {
    this.setData({
      fileList: [...this.data.fileList, val.detail]
    });
  },

  uploadClose: function () {
    this.setData({
      isUpload: false
    });
  },

  publish() {
    const { title, details, id, checked, fileList, disabled } = this.data;
    if(disabled) return;
    const pamars = { title, details, opens: checked ? 1 : 0, picture: fileList.join(",") };
    if(id) pamars.id = id;
    this.setData({ disabled: true });
    const that = this;
    wx.showLoading();
    wx.request({
      url: `${app.globalData.baseUrl}/api/memo/${ id ? 'updateMemo' : 'saveMemo'}`,
      method: "POST",
      header: {
        'token': wx.getStorageSync('token'),
        "Content-Type": "application/json"
      },
      data: pamars,
      success: (res) => {
        if (res.data.code == 0) {
          this.setData({ details: '', title: '' });
          if(wx.getStorageSync('editMemorandum')) {
            wx.removeStorageSync('editMemorandum');
          }
          if(wx.getStorageSync('editMemorandumTitle')) {
            wx.removeStorageSync('editMemorandumTitle');
          }
          wx.navigateBack();
        }
      },
      complete: () => {
        setTimeout(() => {
          that.setData({ disabled: false });
          wx.hideLoading();
        },1000)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})